#  This is part of Scopia Visual Framework
#
#
#  Copyright (C) 2008  Scopia Visual Interfaces Systems 
#                      http://www.scopia.es
#
#  author: Alejandro Conty
#  contributors:
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
from OpenGL.GL import glViewport
from OpenGL.Tk import *
from Tkinter import _default_root
from adaptors import *

ESCKEY = '\x1b'

class tkAdaptor_t(adaptor_t, Widget):
	def __init__(self, appfunc, master=None, cnf={}, **kw):
		Widget.__init__(self, master, 'togl', cnf, kw)
		adaptor_t.__init__(self)
		self.bind('<Map>', self.tkRedraw)
		self.bind('<Expose>', self.tkRedraw)
		self.bind('<Configure>', self.tkRedraw)
		self.bind('<Key>', lambda e:self.keyboard(e.char) )
		self.bind('<Button-1>', lambda e: self.mouseDown(0, e.x, e.y) )
		self.bind('<Button-2>', lambda e: self.mouseDown(1, e.x, e.y) )
		self.bind('<Button-3>', lambda e: self.mouseDown(2, e.x, e.y) )
		self.bind('<Button-4>', lambda e: self.mouseDown(3, e.x, e.y) )
		self.bind('<Button-5>', lambda e: self.mouseDown(4, e.x, e.y) )
		self.bind('<MouseWheel>', self.wheel )
		self.bind('<ButtonRelease-1>', lambda e: self.mouseUp(0,e.x, e.y) )
		self.bind('<ButtonRelease-2>', lambda e: self.mouseUp(0,e.x, e.y) )
		self.bind('<ButtonRelease-3>', lambda e: self.mouseUp(0,e.x, e.y) )
		self.bind('<B1-Motion>', lambda e: self.motion(e.x, e.y) )
		self.bind('<B2-Motion>', lambda e: self.motion(e.x, e.y) )
		self.bind('<B3-Motion>', lambda e: self.motion(e.x, e.y) )
		self.appfunc = appfunc
		self.initialised = False
		_default_root.after(10, self.timer )

	def initialize(self):
		w = self.winfo_width()
		h = self.winfo_height()
		self.app = self.appfunc(w, h)
		del self.appfunc
		self.app.forceDisplay = lambda : self.event_generate('<Expose>')
		self.initialised = True

	def activate(self): self.tk.call(self._w, 'makecurrent')

	def wheel(self, e):
		x, y = e.x, e.y
		if e.delta > 0: self.app.mouse( (False, False,  False, True, False), True, x, y)
		elif e.delta < 0: self.app.mouse( (False, False,  False, False, True), True, x, y)

	def mouseDown(self, button, x, y):
		self.focus_set()
		if button == 0:   b = (True,  False, False, False, False)
		elif button == 1: b = (False, True,  False, False, False)
		elif button == 2: b = (False, False, True,  False, False)
		elif button == 3: b = (False, False,  False, True, False)
		elif button == 4: b = (False, False,  False, False, True)
		else: b = (False, False, False, False, False)
		down = True
		self.app.mouse( b, down, x, y)
	
	def mouseUp(self, button, x, y ):
		if button == 0:     b = (True,  False, False, False, False)
		elif button == 1:  b = (False, False, True, False, False)
		elif button == 2: b = (False, True,  False, False, False)
		elif button == 3: b = (False, True,  False, True, False)
		elif button == 4: b = (False, True,  False, False, True)
		else: b = (False, False, False, False, False)
		down = False
		self.app.mouse( b, down, x, y)
	
	def motion(self, x, y): self.app.motion(x, y)

	def keyboard(self, key): self.app.keyboard( key, 0, 0 )

	def tkRedraw(self, *dummy):
		self.activate()
		if not self.initialised: self.initialize()
		self.update_idletasks()
		w = self.winfo_width()
		h = self.winfo_height()
		self.app.width , self.app.height = w, h
		glViewport(0, 0, w, h)

		self.countFrames()
		self.app._display()

		self.tk.call(self._w, 'swapbuffers')

	def timer(self):
		self.app.timer()
		_default_root.after(10, self.timer )

